﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="edit.aspx.cs" Inherits="OpenRAMS.Community.edit" %>
<%@ Import Namespace="OpenRAMS.Model" %>

<!DOCTYPE html />

<script runat="server">
    void btnCommBookRequest_Click(Object sender, EventArgs e)
    {
        
        

        Booking newBooking = new Booking();
        int anID = Convert.ToInt32(Request.QueryString["bID"].ToString());
        newBooking.Load(anID);

        
        //Fix apostrophes
        String titleFix = sumRequest.Text;
        titleFix = titleFix.Replace("'", @"''");
        newBooking.bookTitle = titleFix;

        String descFix = detRequest.Text;
        descFix = descFix.Replace("'", @"''");
        descFix = descFix.Replace(Environment.NewLine, "<br/>");
        newBooking.bookDesc = descFix;
        
        //Community-dept specific variables set
        newBooking.bookApproved = true;

        //If no date was selected, default to today
        DateTime selected;
        if (dtRequest.SelectedDate == DateTime.Parse("01/01/0001"))
        {
            selected = DateTime.Today;
        }
        else
        {
            selected = dtRequest.SelectedDate;
        }

        //test end time is larger than start time, and date set is either today or in the future.
        if (DateTime.Parse(eTimeRequest.Text) <= DateTime.Parse(sTimeRequest.Text) || selected < DateTime.Today)
        {
            lblError.Visible = true;
            lblError.Text = "Error saving record. (Did you set the dates and times correctly?)";

        }
        else
        {
            
            newBooking.bookStart = new DateTime(selected.Year, selected.Month, selected.Day, DateTime.Parse(sTimeRequest.Text).Hour, DateTime.Parse(sTimeRequest.Text).Minute, 0);
            newBooking.bookEnd = new DateTime(selected.Year, selected.Month, selected.Day, DateTime.Parse(eTimeRequest.Text).Hour, DateTime.Parse(eTimeRequest.Text).Minute, 0);

            int saveBooking = newBooking.Save();
            if (saveBooking < 0)
            {
                lblError.Visible = true;
                lblError.Text = "Error occurred during save (Did you type the wrong thing?)";
            }
            else
            {
              //When we're all done, send back to staff portal
              Response.Redirect("Community.aspx");
            }
        }

    }
    
    void btnCommCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Community.aspx");
    }

    int checkForClash(Booking bk)
    {
        int clashID = -1;

        //Load room requests for this booking
        RoomRequests bookingRoomRequests = new RoomRequests();
        bookingRoomRequests.Populate(bk.bookID);
        List<RoomRequest> roomRequests = bookingRoomRequests.getRoomRequests();

        //for each of those rooms...
        foreach (RoomRequest request in roomRequests)
        {
            //grab other active & approved bookings ready for comparison
            Bookings testBookingList = new Bookings();
            testBookingList.Populate("wontExist", "bitActive='true' AND bitApproved='true'");  //grabs active & approved bookings
            List<Booking> testBookings = testBookingList.getBookingList();

            //Check if they request access to the same room.
            foreach (Booking testBooking in testBookings)
            {
                RoomRequests testRequestList = new RoomRequests();
                testRequestList.Populate(testBooking.bookID);
                List<RoomRequest> testRequests = testRequestList.getRoomRequests();

                foreach (RoomRequest testRequest in testRequests)
                {
                    if (testRequest.RoomName == request.RoomName)
                    {
                        //now we know both bookings are requesting access to the same room, we can now perform tests on them both
                        //I have used 4 separate ifs for comment & readability purposes only

                        //Checks booking doesn't clash with a larger existing booking
                        if (testBooking.bookStart < bk.bookStart && testBooking.bookEnd > bk.bookEnd)
                        {
                            clashID = testBooking.bookID;
                            return clashID;
                        }

                        //Checks booking doesn't clash with a smaller existing booking (or bookings of same duration occurring @ same size)
                        if (testBooking.bookEnd <= bk.bookEnd && testBooking.bookStart >= bk.bookStart)
                        {
                            clashID = testBooking.bookID;
                            return clashID;
                        }

                        //Check if test booking starts in the middle of this one
                        if (testBooking.bookEnd > bk.bookEnd && testBooking.bookStart < bk.bookEnd)
                        {
                            clashID = testBooking.bookID;
                            return clashID;
                        }

                        //Check if test booking ends in the middle of this one
                        if (testBooking.bookEnd < bk.bookEnd && testBooking.bookStart > bk.bookStart)
                        {
                            clashID = testBooking.bookID;
                            return clashID;
                        }

                        //If the test booking has gotten this far, there is no clash
                        
                    }
                }
            }

            
        }
        return clashID;
    }


</script>

<html>
<head id="Head1" runat="server">
    <title>OpenRAMS - Request Booking</title>
    <link rel="Stylesheet" type="text/css" href="../Include/default.css" />
    <style type="text/css">
        .style1
        {
            width: 220px;
        }
    </style>
    <%
        //This page should never render in the browser.

        //Verify a parameter was sent
        String approveRequest = "";
        try
        {
            approveRequest = Request.QueryString["bID"].ToString();
        }
        catch { Response.Redirect("Community.aspx"); }

        //verify parameter sent is of integer type
        int bookingID = -1;
        try
        {
            bookingID = Convert.ToInt32(approveRequest);
        }
        catch { Response.Redirect("Community.aspx"); }

        //Verify user is in Community role (if they aren't send them back to home)
        if (!User.IsInRole("community") && !User.IsInRole("admin")) { Response.Redirect("~/Staff/Staff.aspx"); }

        //Attempt to find the booking
        Booking bookingToApprove = new Booking();
        int result = bookingToApprove.Load(bookingID);

        //if not found, send back
        if (result < 0) { Response.Redirect("Community.aspx"); }
        
        //If all is good by this point, set values for all user controls

        //Outputs logged-in username in string
        lblUserName.Text = bookingToApprove.bookCreator;

        if (!IsPostBack)
        {
            dtRequest.TodaysDate = DateTime.Parse(bookingToApprove.bookStart.ToString("yyyy-MM-dd"));
            dtRequest.SelectedDate = dtRequest.TodaysDate;

            sumRequest.Text = bookingToApprove.bookTitle;
            detRequest.Text = bookingToApprove.bookDesc;
            sTimeRequest.Text = bookingToApprove.bookStart.ToString("HH:mm");
            eTimeRequest.Text = bookingToApprove.bookEnd.ToString("HH:mm");
        }
        
%>
</head>
<body>
    <table class="tableDefault" align="center" width="90%">
    <tr><td><div class="tableTitle" align="center">Request Booking</div></td></tr>
    <tr>
    <td>
    <div class="tableBody">
    <form id="frmBookingRequest" runat="server">
        <table width="100%">
        <tr><td colspan="4" align="center"><asp:Label ID="lblError" runat="server" ForeColor="Red" Visible="false" />
        <tr>
        <td>Requester:</td>
        <td class="style1" colspan="3"><asp:Label ID="lblUserName" runat="server" Font-Bold="true" /></td>
        </tr>
        <tr>
        <td>Summary:</td>
        <td colspan="3"><asp:TextBox ID="sumRequest" runat="server" width="100%" Text="Type booking summary here." MaxLength="49"/></td>
        </tr>
        <tr>
        <td>Description:</td>
        <td colspan="3"><asp:TextBox ID="detRequest" runat="server" TextMode="MultiLine" width="100%" Height="100px" Text="Type booking description here." MaxLength="149" /></td>
        </tr>
        <tr>
       <td>Select Date:</td>
        <td align="center">
            <asp:Calendar ID="dtRequest" runat="server" BackColor="White" 
                BorderColor="White" Font-Names="Verdana" Font-Size="9pt" 
                ForeColor="Black" Height="190px" Width="350px" BorderWidth="1px" 
                NextPrevFormat="FullMonth">
            <DayHeaderStyle Font-Bold="True" Font-Size="8pt" />
            <NextPrevStyle VerticalAlign="Bottom" Font-Bold="True" Font-Size="8pt" 
                    ForeColor="#333333" />
            <OtherMonthDayStyle ForeColor="#999999" />
            <SelectedDayStyle BackColor="#333399" ForeColor="White" />
            <TitleStyle BackColor="White" BorderColor="Black" 
                Font-Bold="True" BorderWidth="4px" Font-Size="12pt" ForeColor="#333399" />
            <TodayDayStyle BackColor="#CCCCCC" />
            </asp:Calendar>
            </td>
            <td>
            <table width="100%" height="100%">
            <tr>
        <td>Start Time:</td></tr>
        <tr>
        <td width="auto"><asp:TextBox ID="sTimeRequest" runat="server" Text="09:00" TextMode="Time" /></td></tr><tr>
        <td>End Time:</td></tr>
        <tr>
        <td width="auto">
        <asp:TextBox ID="eTimeRequest" runat="server" Text="10:00" TextMode="Time" /></td>
        </tr>
        </table>
        </td>
        </tr>
       
        <tr>
        <td colspan="3">
        <table width="100%">
        <td align="center"><asp:Button ID="btnCommBookRequest" Text="Submit" 
                runat="server" Width="100%" onclick="btnCommBookRequest_Click" /></td>
        <td align="center"><asp:Button ID="btnCommCancel" Text="Cancel" runat="server" width="100%" 
                onclick="btnCommCancel_Click"/></td>
        </tr>
        </table>
        </td>
        </tr>
        </table>
        </form>
    </div>
    </td>
    </tr>
    <tr>
    <td colspan="2"><div class="footer">
    <% 
        Response.WriteFile(Server.MapPath("~/Include/footer.txt"));
    %>
    </div></td>
    </tr>
    </table>
</body>
</html>
